#pragma once
#include<iostream>
#include"Protocal.hpp"

enum{
    Div_Zero=1,
    Mod_Zero,
    Other_Oper
};

class ServerCal
{
public:
    ServerCal()
    {}
    Response CalculatorHelper(const Request &req)
    {
        Response resp(0,0);
        switch(req.op)
        {
            case '+':
                resp.result=req.x+req.y;
                break;
            case '-':
                resp.result=req.x-req.y;
                break;
            case '*':
                resp.result=req.x*req.y;
                break;
            case '/':
            {
                if(req.y==0)
                    resp.code=Mod_Zero;
                else 
                    resp.result=req.x/req.y;
            }
            break;
            case '%':
            {
                if(req.y==0) resp.code=Mod_Zero;
                else resp.result=req.x % req.y;
            }
            break;
            default:
                resp.code=Other_Oper;
                break;
        }
        return resp;
    }
    string Calculator(string &package)
    {
        string content;
        bool r=Decode(package,&content);// "len"\n"10 + 20"\n，解析网络请求的字符串
        if(!r) return ""; 
        Request req;
        r=req.Deserialize(content);// "10 + 20" ->x=10 op=+ y=20
        if(!r) return "";
        content="";//对content清零
        Response resp=CalculatorHelper(req);//进行网络计算
        resp.Serialize(&content);//打包计算完后的结果并返回
        content=Encode(content);
        return content;
    }
    ~ServerCal()
    {}
};